<template>
  <div class="list">
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home'}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="10">
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="params.query" clearable @clear="queryAll">
            <el-button slot="append" icon="el-icon-search" @click="queryGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="100px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="100px"></el-table-column>
        <el-table-column label="创建时间" width="150px">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button type="primary" class="el-icon-edit" size="mini" @click="editGoods(scope.row)"></el-button>
            <el-button type="danger" class="el-icon-delete" size="mini" @click="deleteGoods(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="params.pagenum" :page-sizes="pageSizes" :page-size="params.pagesize" :total="total" layout="total, sizes, prev, pager, next, jumper" background></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      params: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      pageSizes: [5, 10, 15, 20],
      total: 0,
      goodsList: []
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', { params: this.params })
      if(res.meta.status !== 200) return this.$message({ type: 'error', message: '获取商品列表失败', duration: 2000 })
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    queryGoodsList() {
      this.params.pagenum = 1
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      this.params.pagenum = val
      this.getGoodsList()
    },
    handleSizeChange(val) {
      this.params.pagesize = val
      this.getGoodsList()
    },
    queryAll() {
      this.getGoodsList()
    },
    async deleteGoods(info) {
      const flag = await this.$confirm('你确定要删除该商品吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(info)
      if(flag !== 'confirm') return this.$message({ type: 'info', message: '已取消删除操作', duration: 2000 })
      const { data: res } = await this.$http.delete('goods/' + info.goods_id)
      if(res.meta.status !== 200) return this.$message({ type: 'error', message: '删除失败', duration: 2000 })
      this.getGoodsList()
    },
    editGoods(info) {
      this.$router.push({ name: 'Edit', params: info })
    },
    goAddPage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
  /deep/ .el-input__inner{
    height: 39px;
  }
</style>
